package com.city.ucenterservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.city.commonutils.JwtUtils;
import com.city.commonutils.MD5;
import com.city.servicebase.myexception.MyException;
import com.city.servicebase.myexception.exceptionenum.MyExceptionEnumImpl;
import com.city.ucenterservice.entity.UcenterMember;
import com.city.ucenterservice.entity.vo.RegisteredVo;
import com.city.ucenterservice.mapper.UcenterMemberMapper;
import com.city.ucenterservice.service.UcenterMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author city
 * @since 2020-10-01
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;//springboot整和redis的工具类

    @Override
    public String login(UcenterMember ucenterMember) {
        //登录判断账号密码，是否可用，和token
        String mobile = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();
        if (StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
            throw new MyException(MyExceptionEnumImpl.USER_AND_PASS_NOT);
        }
        QueryWrapper<UcenterMember> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        UcenterMember ucenterMember1 = baseMapper.selectOne(queryWrapper);
        if (ucenterMember1==null) {
            throw new MyException(MyExceptionEnumImpl.NOT_USER);
        }

        //与加密后得密码对比
        if (!MD5.encrypt(password).equals(ucenterMember1.getPassword())){
            throw new MyException(MyExceptionEnumImpl.NOT_PASS);
        }
        if (ucenterMember1.getIsDisabled()){
            throw new MyException(MyExceptionEnumImpl.NOT_DISABLED);
        }
        //登录成功，生成token
        String jwtToken = JwtUtils.getJwtToken(ucenterMember1.getId(), ucenterMember1.getNickname());

        return jwtToken;
    }

    @Override
    public void register(RegisteredVo registeredVo) {
        String code = registeredVo.getCode();//验证码
        String mobile = registeredVo.getMobile();//手机号
        String nickname = registeredVo.getNickname();//用户昵称
        String password = registeredVo.getPassword();//用户密码
        if (StringUtils.isEmpty(code)||StringUtils.isEmpty(mobile)||StringUtils.isEmpty(nickname)||StringUtils.isEmpty(password)){
            throw new MyException(MyExceptionEnumImpl.NOT_INFO_REGISTERED);
        }
        //判断验证码
        String s = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(s)){
            throw new MyException(MyExceptionEnumImpl.CODE_ERROR);
        }

        //判断手机号是否以注册
        QueryWrapper<UcenterMember> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        Integer integer = baseMapper.selectCount(queryWrapper);
        if (integer > 0) {
            throw new MyException(MyExceptionEnumImpl.PHONE_REPEAT);
        }

        //插入用户
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(mobile);
        ucenterMember.setNickname(nickname);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setIsDisabled(false);
        ucenterMember.setAvatar("https://hello-edu.oss-cn-beijing.aliyuncs.com/0.jpg");
        baseMapper.insert(ucenterMember);
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("openid",openid);
        UcenterMember ucenterMember = baseMapper.selectOne(queryWrapper);
        return ucenterMember;
    }

    @Override
    public int getCountRegister(String day) {
        return baseMapper.getCount(day);
    }
}
